/*****************************************************************************
 * plum_section.h
 *
 * Copyright (C) 2020 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

#ifndef __PLUM_SECTION_H__
#define __PLUM_SECTION_H__
/*****************************************************************************
 * Included Files
 ****************************************************************************/

#include "plum_micro.h"

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
/**
 * @def PLUM_SECTION_ALIGNMENT
 * @brief The operation of acquiring the stored symbol relies on the size of
 *        the stored type
 */
#define PLUM_SECTION_ALIGNMENT PLUM_ALIGNED(sizeof(void *) * 2)

#if defined(__CC_ARM)
#warning "compiler is CC_ARM"
#define PLUM_SECTION_START_ADDR(section_name) \
    &PLUM_CONTACT(section_name, $$Base)

#define PLUM_SECTION_END_ADDR(section_name) &PLUM_CONTACT(section_name, $$Limit)

#define PLUM_SECTION_DEF(section_name, type)         \
    extern type *PLUM_CONTACT(section_name, $$Base); \
    extern void *PLUM_CONTACT(section_name, $$Limit)

#define PLUM_SECTION_REF(section_name, type)  \
    type *PLUM_CONTACT(section_name, $$Base); \
    void *PLUM_CONTACT(section_name, $$Limit)

#define PLUM_SECTION_ITEM_REGISTER(section_name, section_var)       \
    section_var __attribute__((section(PLUM_STRING(section_name)))) \
        __attribute__((used))

#elif defined(__GNUC__)
//#warning "compiler is GUNC"
#define PLUM_SECTION_START_ADDR(section_name) \
    &PLUM_CONTACT(__start_, section_name)

#define PLUM_SECTION_END_ADDR(section_name) &PLUM_CONTACT(__stop_, section_name)

#define PLUM_SECTION_DEF(section_name, type)           \
    extern type *PLUM_CONTACT(__start_, section_name); \
    extern void *PLUM_CONTACT(__stop_, section_name)

#define PLUM_SECTION_REF(section_name, type)    \
    type *PLUM_CONTACT(__start_, section_name); \
    void *PLUM_CONTACT(__stop_, section_name)

#define PLUM_SECTION_ITEM_REGISTER(section_name, section_var)       \
    section_var __attribute__((section(PLUM_STRING(section_name)))) \
        __attribute__((used))

#elif defined(__ICCARM__)
#warning "compiler is ICCARM"
#define PLUM_SECTION_START_ADDR(section_name) \
    __section_begin(PLUM_STRING(section_name))

#define PLUM_SECTION_END_ADDR(section_name) \
    __section_end(PLUM_STRING(section_name))

#define PLUM_SECTION_DEF(section_name, type) \
    _Pragma(PLUM_STRING(section = PLUM_STRING(section_name)));

#define PLUM_SECTION_ITEM_REGISTER(section_name, section_var) \
    __root section_var @PLUM_STRING(section_name)

#else
#error Unknown compiler
#endif

/**
 * @def PLUM_SECTION_LENGTH
 * @brief Macro for retrieving the length of a given section, in bytes.
 *
 * @param[in]: section_name - The Name of the section.
 *
 * @return The length of a given section, in bytes.
 */
#define PLUM_SECTION_LENGTH(section_name)          \
    ((size_t)PLUM_SECTION_END_ADDR(section_name) - \
     (size_t)PLUM_SECTION_START_ADDR(section_name))

/**
 * @def PLUM_SECTION_ITEM_GET
 * @brief Macro for retrieving a variable from a section.
 *
 * @warning     The stored symbol can only be resolved using this macro if the
 *              type of the data is word aligned. The operation of acquiring
 *              the stored symbol relies on the size of the stored type. No
 *              padding can exist in the named section in between individual
 *              stored items or this macro will fail.
 *
 * @param[in]: section_name - The name of the section.
 * @param[in]: data_type    - Data type of the variable.
 * @param[in]: i            - The index of the variable in section.
 */
#define PLUM_SECTION_ITEM_GET(section_name, data_type, i) \
    ((data_type *)PLUM_SECTION_START_ADDR(section_name) + (i))

/**
 * @def PLUM_SECTION_ITEM_COUNT
 * @brief Macro for getting the number of variables in a section.
 *
 * @param[in]: section_name - The name of the section.
 * @param[in]: data_type    - Data type of the variable.
 */
#define PLUM_SECTION_ITEM_COUNT(section_name, data_type) \
    PLUM_SECTION_LENGTH(section_name) / sizeof(data_type)

/*****************************************************************************
 * Public Types
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/*****************************************************************************
 * Public Function Prototypes
 ****************************************************************************/

/****************************************************************************/
#ifdef __cplusplus
}
#endif
/****************************************************************************/
#endif /* __PLUM_SECTION_H__ */
/****************************************************************************/

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
